/*
 *    Copyright 2011 The gcal-to-refill project
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.googlecode.gcal2refill;

import java.util.Calendar;
import java.util.Date;

import com.google.gdata.data.extensions.When;
import com.itextpdf.text.Rectangle;

public class OneDayAreaInfo {

	public float getEventEreaHeight() {
		return eventAreaHeight;
	}

	public Rectangle getOneDayRect() {
		return oneDayRect;
	}

	public OneDayAreaInfo(UserInput ui) {
		int tojisiroSize = 0;
		vrticalNum = ui.getVirticalPeriod() - ui.getVirticalStartTime();
		oneDayEventNum = ui.getOneDayEventNum();
		if (ui.isTojisiro())
			tojisiroSize = ui.getTojisiroSize();
		oneDayRect = new Rectangle(0.0F, 0.0F,
				(PdfCreator.pageSize[ui.getSizeOfPaper()].getWidth() - Util
						.mm2point(tojisiroSize)) / 4F,
				PdfCreator.pageSize[ui.getSizeOfPaper()].getHeight());
		eventAreaHeight = DAYINFO_HEIGHT + ui.getEventFontSize()
				* (float) ui.getOneDayEventNum() + 2.0F;
		vrticalHeight = (oneDayRect.getHeight() - eventAreaHeight)
				/ (float) vrticalNum;
	}

	public float getVirticalPositionHeight(int num) {
		return (oneDayRect.getHeight() - eventAreaHeight)
				- (vrticalHeight * (float) num);
	}

	public int getVrticalNum() {
		return vrticalNum;
	}

	public int getOneDayEventNum() {
		return oneDayEventNum;
	}

	public float getVrticalHeight() {
		return vrticalHeight;
	}

	public float getTime2Height(UserInput userIn, When when, Date createDate) {
		Calendar calCreate = Calendar.getInstance();
		Calendar calStart = Calendar.getInstance();
		Calendar calEnd = Calendar.getInstance();
		calCreate.setTime(createDate);
		calCreate.set(Calendar.HOUR_OF_DAY, userIn.getVirticalStartTime());
		calStart.setTime(new Date(when.getStartTime().getValue()));
		calEnd.setTime(new Date(when.getEndTime().getValue()));
		if (calCreate.after(calStart)) {
			calStart.setTime(createDate);
			calStart.set(Calendar.HOUR_OF_DAY, userIn.getVirticalStartTime());
			calStart.set(Calendar.MINUTE, 0);
		}
		calCreate.add(Calendar.DAY_OF_YEAR, 1);
		if (calCreate.before(calEnd)) {
			calEnd.setTime(createDate);
			calEnd.set(Calendar.HOUR_OF_DAY, userIn.getVirticalPeriod());
		}
		long interval = calEnd.getTimeInMillis() - calStart.getTimeInMillis();
		int intarvalHour = (int) (interval / 1000L / 60L / 60L);
		int intarvalMinute = (int) ((interval - (long) (intarvalHour * 60 * 60 * 1000)) / 1000L / 60L);
		float ret = ((float) intarvalHour + (float) intarvalMinute / 60F)
				* vrticalHeight;
		return ret;
	}

	public float getStartTimePositionHeight(UserInput userIn, When when,
			Date createDate) {
		Calendar calCreate = Calendar.getInstance();
		Calendar calStart = Calendar.getInstance();
		calCreate.setTime(createDate);
		calCreate.set(Calendar.HOUR_OF_DAY, userIn.getVirticalStartTime());
		calStart.setTime(new Date(when.getStartTime().getValue()));
		calCreate.set(Calendar.HOUR_OF_DAY, 0);
		calCreate.set(Calendar.MINUTE, 0);
		calCreate.set(Calendar.SECOND, 0);
		calCreate.set(Calendar.MILLISECOND, 0);
		if (calCreate.after(calStart))
			calStart.setTime(createDate);
		if (calStart.get(Calendar.HOUR_OF_DAY) < userIn.getVirticalStartTime())
			calStart.set(Calendar.HOUR_OF_DAY, userIn.getVirticalStartTime());
		int hour = calStart.get(Calendar.HOUR_OF_DAY);
		int minute = calStart.get(Calendar.MINUTE);
		int lineNum = hour - userIn.getVirticalStartTime();
		if (lineNum < 0)
			lineNum = 0;
		float ret = getVirticalPositionHeight(lineNum);
		ret -= ((float) minute / 60F) * vrticalHeight;
		return ret;
	}

	public static float DAYINFO_HEIGHT = 18F;
	private Rectangle oneDayRect;
	private int oneDayEventNum;
	private int vrticalNum;
	private float vrticalHeight;
	private float eventAreaHeight;

}
